﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.HttpLoadBalancer.HttpProtocal;

namespace A2DFramework.HttpLoadBalancer.Parsers.ConcreteParsers
{
    class HttpRequestParser : IHttpRequestParser
    {
        public HttpProtocal.HttpRequestEntity Parse(string rawHttpText)
        {
            if (rawHttpText == null || rawHttpText.Trim().Length == 0)
                return null;

            string headerString;
            string bodyString;
            string requestLineString;

            string[] parsedString=Helper.ParseHeaderBody(rawHttpText);

            requestLineString = parsedString[0];
            headerString=parsedString[1];
            bodyString = parsedString[2];

            HttpProtocal.HttpRequestEntity request = new HttpProtocal.HttpRequestEntity();


            request.RequestLine = Helper.ParseRequestLine(requestLineString);
            request.HttpHeaders = Helper.ParseHeaders(headerString);


            //request.HttpBody=string.Empty;

            //if (request.RequestLine.Action == HttpProtocal.HttpRequestActions.GET
            //    || request.RequestLine.Action == HttpProtocal.HttpRequestActions.POST
            //    || request.RequestLine.Action == HttpProtocal.HttpRequestActions.PUT)
            //{
            //    request.HttpBody = bodyString;
            //}

            return request;
        }
    }
}
